package liuch.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 功能描述: <br>
 * 对于重入锁而言，"lock"和"keep"是两个不同的概念。lock了锁，不一定keep锁，但keep了锁一定已经lock了锁
 *
 * @data 2018/8/17/017
 * @author 17101651
 */
public class ReetrantLockTest {
    public static void main(String[] args){
        final ExecutorService executorService = Executors.newFixedThreadPool(4);
        final ReentrantLock lock = new ReentrantLock();
        final Condition con = lock.newCondition();
        final int time = 5;
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("Pre" + lock);
                lock.lock();
                try {
                    con.await(time, TimeUnit.SECONDS);
//                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println("Post" + lock.toString());
                    lock.unlock();
                }
            }
        };
        for (int i = 0; i < 4; i++) {
            executorService.submit(runnable);
        }
        executorService.shutdown();
    }
}
